﻿using Android.App;
using Android.Content;
using Android.Media.Session;

namespace TestActionBar
{
	public class MediaEvent : MediaSession.Callback
	{
		public   Notification.Action CreateAction (int icon, string title, string intentAction)
		{
			Intent intent = new Intent (MediaPlayerService.mContext, typeof(MediaPlayerService));
			intent.SetAction (intentAction);
			PendingIntent pendingIntent = PendingIntent.GetService (MediaPlayerService.mContext, 1, intent, 0);
			return new Notification.Action.Builder (icon, title, pendingIntent).Build ();

		}

		public   void BuildNotification (Notification.Action action)
		{
			Notification.MediaStyle style = new Notification.MediaStyle ();
			style.SetMediaSession (MediaPlayerService.m_objMediaSession.SessionToken);
			style.SetShowActionsInCompactView (2);

			Intent intent = new Intent (MediaPlayerService.mContext, typeof(MediaPlayerService));
			intent.SetAction ("action_stop");

			Notification.Builder builder = new Notification.Builder (MediaPlayerService.mContext)
				.SetSmallIcon (Resource.Drawable.Icon)
				.SetContentTitle ("Sample Title")
				.SetContentText ("Sample Artist")
				.SetAutoCancel (false)
				.SetShowWhen (false) // hide timestamp
				.SetStyle (style);


			builder.AddAction (CreateAction (Android.Resource.Drawable.IcMediaPrevious, "Previous", Constants.ACTION_PREVIOUS));
			builder.AddAction (CreateAction (Android.Resource.Drawable.IcMediaRew, "Rewind", Constants.ACTION_REWIND));
			builder.AddAction (action);
			builder.AddAction (CreateAction (Android.Resource.Drawable.IcMediaFf, "Fast Foward", Constants.ACTION_FAST_FORWARD));
			builder.AddAction (CreateAction (Android.Resource.Drawable.IcMediaNext, "Next", Constants.ACTION_NEXT));


			NotificationManager notificationManager = (NotificationManager)MediaPlayerService.mContext.GetSystemService (Context.NotificationService);
			notificationManager.Notify (1, builder.Build ());
			builder.SetVisibility (NotificationVisibility.Public);
		}

		public override void OnPlay ()
		{
			base.OnPlay ();
			BuildNotification (CreateAction (Android.Resource.Drawable.IcMediaPause, "Pause", Constants.ACTION_PAUSE));
		}

					 
		public override void OnPause ()
		{
			base.OnPause ();
			BuildNotification (CreateAction (Android.Resource.Drawable.IcMediaPlay, "Play", Constants.ACTION_PLAY));
		}

					 
		public override void OnSkipToNext ()
		{
			base.OnSkipToNext ();
			BuildNotification (CreateAction (Android.Resource.Drawable.IcMediaNext, "Next", Constants.ACTION_NEXT));
		}

						 
		public override void OnSkipToPrevious ()
		{
			base.OnSkipToPrevious ();
			BuildNotification (CreateAction (Android.Resource.Drawable.IcMediaPause, "Prev", Constants.ACTION_PREVIOUS));
		}
	}
}

